package cn.fsd.service.impl;

import cn.fsd.domain.Commodity;
import cn.fsd.domain.Station;
import cn.fsd.domain.Storeroom;
import cn.fsd.domain.Storeroom_commodity;
import cn.fsd.mapper.CommodityMapper;
import cn.fsd.mapper.StationMapper;
import cn.fsd.mapper.StoreroomMapper;
import cn.fsd.mapper.Storeroom_commodityMapper;
import cn.fsd.query.delivery.StoreroomQuery;
import cn.fsd.service.StoreRoomService;
import cn.fsd.util.CacheConstant;
import cn.fsd.util.Constants;
import cn.fsd.vo.PageBean;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
@CacheConfig(cacheNames = CacheConstant.STOREROOM_CACHE_NAME)
public class StoreRoomServiceImpl implements StoreRoomService {

  @Autowired
  StoreroomMapper storeroomMapper;

  @Resource
  CommodityMapper commodityMapper;

  @Resource
  Storeroom_commodityMapper storeroomCommodityMapper;

  @Resource
  StationMapper stationMapper;

  @Override
  public Storeroom getStoreRoomByName(String name) {
    return storeroomMapper.selectByStoreroomName(name);
  }

  @Cacheable(key = "'" + CacheConstant.STOREROOM_CACHE_KEY_PREFIX
      + "' + #id", unless = "#result == null")
  @Override
  public Storeroom getStoreRoomByID(String id) {
    return storeroomMapper.selectByPrimaryKey(id);
  }

  @Override
  public String addStoreRoom(Storeroom storeroom) {
    String id = UUID.randomUUID().toString();
    storeroom.setStoreroom_id(id);
    storeroomMapper.insert(storeroom);
    //新建库房要放入所有商品
    List<Commodity> commodityList = commodityMapper.selectAll();
    storeroomCommodityMapper.addAllCommodityInNewStoreroom(storeroom.getStoreroom_id(),commodityList);
    //在satation里面加一条数据
    if(Objects.equals(storeroom.getStoreroom_level(), Constants.SUBSTATION)){
      Station station = new Station(storeroom.getStoreroom_id(),storeroom.getStoreroom_place(),storeroom.getStoreroom_name(),storeroom.getStoreroom_phone());
      stationMapper.insert(station);
    }
    return id;
  }

  @Override
  public PageBean<Storeroom> findStoreRoomByPage(StoreroomQuery query) {
    PageBean<Storeroom> pageBean = new PageBean<>();

    PageHelper.startPage(query.getPageNow(),query.getPageSize());

    List<Storeroom> storeroomList = storeroomMapper.findStoreroomByPage(query);

    PageInfo<Storeroom> pageInfo = new PageInfo<>(storeroomList);

    pageBean.setTotalPages(pageInfo.getPages());
    pageBean.setTotalRows(pageInfo.getTotal());
    pageBean.setRowData(pageInfo.getList());

    return pageBean;
  }

  @CacheEvict(key = "'" + CacheConstant.STOREROOM_CACHE_KEY_PREFIX
      + "' + #id")
  @Override
  public int delStoreroomByID(String id) {
    Storeroom_commodity storeroomCommodity = getStoreValue(id);
    if(storeroomCommodity!=null&&storeroomCommodity.getTransferred_num()+storeroomCommodity.getTransferable_num()+storeroomCommodity.getTotal_num()+storeroomCommodity.getExchanged_num()!=0){
      return -1;
    }
    storeroomMapper.deleteByPrimaryKey(id);
    return 0;
  }

  @CacheEvict(key = "'" + CacheConstant.STOREROOM_CACHE_KEY_PREFIX
      + "' + #storeroom.storeroom_id")
  @Override
  public void updateStoreroom(Storeroom storeroom) {
    storeroomMapper.updateByPrimaryKey(storeroom);
  }

  @Override
  public Storeroom_commodity getStoreValue(String storeroom_id) {
    return storeroomCommodityMapper.getStoreValue(storeroom_id);
  }
}
